<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class flag_model extends MY_Model {

	protected $table		= "flag";
	protected $table2		= "flag_company";
	protected $table3		= "flag_role";
	protected $table4		= "flag_contact";
	protected $key			= "id";
	protected $soft_deletes	= true;
	protected $date_format	= "datetime";
	protected $set_created	= false;
	protected $set_modified = false;
	
	protected $orderby 		= "name_ar";

	//---------------------------------------------------------------
	
	function flag_company($form)
	{
		// Delete all records regarding to company id
		$result = $this->db->delete($this->table2, array('company_id' => $form['company_id']));
		if ($result)
		{
			$del = true;
		}

		//Add New rows
		$result = true;
		if(is_array($form['flag_id'])){
		foreach($form['flag_id'] as $row){
			$result = $this->db->insert($this->table2, array('company_id'=>$form['company_id'], 'flag_id'=>$row));
			if(!$result)
				return false;
		}}
		return True;
	} 
	
	function get_flag_company($com_id)
	{
		$this->db->select('flag_company.* , flag.name_en ,flag.name_ar');
		$this->db->from($this->table2);
		$this->db->where('company_id', $com_id);
		$this->db->join($this->table,'flag_company.flag_id = flag.id');
		$result = $this->db->get();
		
		if($result->num_rows() > 0 ){
			return $result->result();
		}
		
		return false;
		
	}
	
	function flag_role($form)
	{
		// Delete all records regarding to role id
		$result = $this->db->delete($this->table3, array('role_id' => $form['role_id']));
		if ($result)
		{
			$del = true;
		}

		//Add New rows
		$result = true;
		if(is_array($form['flag_id'])){
		foreach($form['flag_id'] as $row){
			$result = $this->db->insert($this->table3, array('role_id'=>$form['role_id'], 'flag_id'=>$row));
			if(!$result)
				return false;
		}}
		return True;
	} 
	
	function get_flag_role($role_id)
	{
		$this->db->select('flag_role.* , flag.name_en ,flag.name_ar');
		$this->db->from($this->table3);
		$this->db->where('role_id', $role_id);
		$this->db->join($this->table,'flag_role.flag_id = flag.id');
		$result = $this->db->get();
		
		if($result->num_rows() > 0 ){
			return $result->result();
		}
		
		return false;
		
	}
	
	function flag_contact($form)
	{
		// Delete all records regarding to contact_id id
		$result = $this->db->delete($this->table4, array('contact_id' => $form['contact_id']));
		if ($result)
		{
			$del = true;
		}

		//Add New rows
		$result = true;
		if(is_array($form['flag_id'])){
		foreach($form['flag_id'] as $row){
			$result = $this->db->insert($this->table4, array('contact_id'=>$form['contact_id'], 'flag_id'=>$row));
			if(!$result)
				return false;
		}}
		return True;
	} 

	function get_flag_contact($contact_id)
	{
		$this->db->select('flag_contact.* , flag.name_en ,flag.name_ar');
		$this->db->from($this->table4);
		$this->db->where('contact_id', $contact_id);
		$this->db->join($this->table,'flag_contact.flag_id = flag.id');
		$result = $this->db->get();
		
		if($result->num_rows() > 0 ){
			return $result->result();
		}
		
		return false;
		
	}
	
	function get_contacts_id_by_flag($flag_list)
	{
		$this->db->select('contact_id');
		$this->db->from($this->table4);
		
		$i = 0; 
		while($i < count($this->allowed_flags_id)){
			if($i==0)
				$this->db->where('flag_id', $this->allowed_flags_id[$i]);
			else
				$this->db->or_where('flag_id', $this->allowed_flags_id[$i]);

			$i++;
		}
			//$this->db->where('flag_id', $flag_list);
			
		$result = $this->db->get();
		
		if($result->num_rows() > 0 ){
			return $result->result();
		}
		
	}
}
